package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import org.osmf.events.TimeEvent;
	
	import view.FPSMeter;
	import view.load.BitmapdataPool;
	import view.role.Role;	
	
	public class TestRole extends Sprite
	{
		private var loader:Loader;
		
		//private var allRoleList_old:Vector.<RoleBitmap>;
		//private var displayList_old:Vector.<RoleBitmap>;
		
		private var allRoleList:Vector.<Role>;
		private var displayList:Vector.<Role>;
		
		private var defaultBimtap:Array = new Array;
		
		
		
		private var baseW:int;
		private var baseH:int;
		private var timeFlag:Number;
		
		private var fps:FPSMeter;
		
		private var flag:Boolean;
		
		private var keyAry:Array = ["FAngel.png","FAngel_2.png","FAngel_3.png",
			"FAngel_4.png","FHuman.png","FHuman_2.png","FHuman_3.png",
			"FHuman_4.png","FMerfolk.png","FMerfolk_2.png","FMerfolk_3.png",
			"FMerfolk_4.png","FTiaer.png","FTiaer_2.png","FTiaer_3.png","FTiaer_4.png"];
		
		private var mainRec:Rectangle;
		//private var roleContainer:Sprite;
		//private var nameContainer:Sprite;
		public function TestRole()
		{
			this.addEventListener(Event.ADDED_TO_STAGE,addTostage);
		}
		private function addTostage(event:Event):void{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			baseW = 1300//stage.stageWidth;
			baseH = 700//stage.stageHeight;
			
			/*roleContainer = new Sprite();
			this.addChild(roleContainer);
			nameContainer = new Sprite();
			this.addChild(nameContainer);*/
			
			mainRec = new Rectangle(0,0,stage.stageWidth,stage.stageHeight);
			fps = new FPSMeter
			this.addChild(fps);
			//loadRole();
			addRole();
		}
		private function addRole():void{
			allRoleList = new Vector.<Role>;
			displayList = new Vector.<Role>;
			var l:int = 3;
			var angle:Number = 2 * Math.PI
			for(var j:int=0;j<2000;j++){
				var role:Role = new Role;
				//role.addTostage(this);
				role.x = baseW*2*Math.random();
				role.y = baseH*2*Math.random();
				var s:Number = angle*Math.random();
				if(j%2){
					role.vx = l * Math.sin(s);
					role.vy = l * Math.cos(s);
				}else{
					role.vx = 0;
					role.vy = 0;
				}
				
				role.dircet = 8*Math.random();
				var obj:Object = new Object;
				obj.title = "神舟" + j + "号";
				obj.image = keyAry[int(16*Math.random())];
				role.info = obj;
				allRoleList.push(role);
				
			}
			
			var timer:Timer = new Timer(500);
			timer.addEventListener(TimerEvent.TIMER,onTimerLogic);
			timer.start();
			
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		/*private function addNewRole():void{
			allRoleList_old = new Vector.<RoleBitmap>;
			displayList_old = new Vector.<RoleBitmap>;
			for(var j:int=0;j<3000;j++){
				var role:RoleBitmap = new RoleBitmap(new BitmapData(120,120));
				this.addChild(role);
				role.x = baseW*2*Math.random();
				role.y = baseH*2*Math.random();
				role.dircet = 8*Math.random();
				role.source = keyAry[int(16*Math.random())]
				allRoleList_old.push(role);
			}
			
			var timer:Timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER,changeDisplay);
			timer.start();
			
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			
		}*/
		
		
		private function changeDisplay():void{
			var tempV:Vector.<Role> = new Vector.<Role>;
			var role:Role;
			for(var i:int=0;i<allRoleList.length;i++){
				role = allRoleList[i];
				if(role.x < baseW && role.y <baseH){
					tempV.push(role);
				}
				
			}
			var tArr:Vector.<Role> = toSetArray(displayList,tempV),
				t2Arr:Vector.<Role> = toSetArray(tempV,displayList);
			for each(role in tArr)
			{
				role.removeFromStage();
			} 
			for each(role in t2Arr)
			{
				role.addTostage(this);
			}	
			displayList = tempV;
			
		}
		private function onTimerLogic(event:TimerEvent):void{
			if(flag){
				changeDisplay();
				fps.setConsole(displayList.length+","+this.numChildren);
				flag = false;
			}else{
				sortLayer();
				flag = true;
			}
			
			
			
		}
		private function toSetArray (arr:Vector.<Role> ,arr1:Vector.<Role> ):Vector.<Role> 
		{
			var l:int = arr.length;
			var temp:Vector.<Role> =new Vector.<Role>;
			for(var i:int=0;i<l;i++)
			{
				if(arr1.indexOf(arr[i])==-1)
					temp[temp.length] = arr[i];  
			}
			return temp; 
		}
		private var si:int;
		private function sortLayer():void
		{
			displayList.sort(compare);
			var index:int = this.numChildren-1;
			for each(var r2:Role in displayList)
			{
				index = r2.SetChildIndex(index);	
			} 
		}
		private function doSort4(maxNum:int):void {
			/*
			var sortedLayers:Array=arrayResult;
			sortedLayers.sortOn("y")
			var item:Sprite=null;
			var child:Sprite=null;
			while (maxNum--) {
				item = sortedLayers[maxNum];
				child = getChildAt(maxNum) as Sprite;
				if (child != item) {
					setNum++
						setChildIndex(sortedLayers[maxNum], maxNum);
				}
			}
			*/
		}
		private function compare(one:Role,two:Role):Number{
			return two.y - one.y;
		}
		//private var flag:int;
		//private var _roleList:Vector.<RoleBitmap> 
		private function onEnterFrame(event:Event):void{
			
			/*_roleList = allList[flag];
			for each(var role:RoleBitmap in _roleList){
				if(role.renderable == timeFlag)
					role.render();
			}
			flag++;
			if(flag == 5){
				flag = 0;
			}*/
			
			for each(var role:Role in displayList){
				role.render();
			}
			for each(role in allRoleList){
				role.x += role.vx;
				role.y += role.vy;
				if(role.x > baseW*2 || role.x < 0 || role.y > baseH*2 || role.y < 0){
					role.x = baseW*2*Math.random();
					role.y = baseH*2*Math.random();
				}
			}
		}
	}
}